package com.cpi.cornpp.biweeks.converter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.biweeks.dto.BiWeekDTO;
import com.cpi.cornpp.biweeks.view.BiWeekView;
import com.cpi.cornpp.biweeks.view.BiWeekViewList;


public class BiWeekConverter {

	private static final String SPACE = " ";//$NON-NLS-1$

	/**
	 * Convierte un objeto del tipo <code>BiWeekDTO</code> a un objeto del tipo <code>BiWeekView</code>
	 * @param dto Objeto del tipo <code>BiWeekDTO</code> a convertir
	 * @return Objeto del tipo <code>BiWeekView</code>
	 */
	public static BiWeekView convert(BiWeekDTO dto){
		BiWeekView view = null;
		Calendar c = null;
		
		if (dto != null){
			c = Calendar.getInstance();
			view = new BiWeekView();
			view.setBiweekPeriodId(dto.getBiweekPeriodId());
			view.setShortName(dto.getShortName());
			view.setDescription(new StringBuffer( dto.getDescription()).append(SPACE).append(dto.getYear()).toString());
			view.setBiweek(dto.getBiweek());
			view.setMonth(dto.getMonth());
			view.setYear(dto.getYear());
			
			if(view.getYear() == c.get(Calendar.YEAR) && 
				view.getMonth() == (c.get(Calendar.MONTH) + 1)){
				if(view.getBiweek() == 1 && c.get(Calendar.DAY_OF_MONTH) <= 15){
					view.setDefaultInit(true);
				}else if (view.getBiweek() == 2 && c.get(Calendar.DAY_OF_MONTH) > 15){
					view.setDefaultInit(true);
				}
				
			}
			
			if(view.getYear()== c.get(Calendar.YEAR) &&
				view.getBiweek()== 2 &&
				view.getMonth() == Calendar.DECEMBER + 1){
				view.setDefaultFinish(true);
			}
			
		}
		
		return view;
	}
	
	/**
	 * Convierte un objeto del tipo <code>BiWeekView</code> a un objeto del tipo <code>BiWeekDTO</code>
	 * @param view Objeto del tipo <code>BiWeekView</code> a convertir
	 * @return Objeto del tipo <code>BiWeekDTO</code>
	 */
	public static BiWeekDTO convert(BiWeekView view){
		BiWeekDTO dto = null;
		
		if (view != null){
			dto = new BiWeekDTO();
			dto.setBiweekPeriodId(view.getBiweekPeriodId());
			dto.setShortName(view.getShortName());
			dto.setDescription(view.getDescription());
			dto.setBiweek(view.getBiweek());
			dto.setMonth(view.getMonth());
			dto.setYear(view.getYear());		
		}
		
		return dto;
	}
	
	/**
	 * 
	 * @param dtoList
	 * @return
	 */
	public static BiWeekViewList convertDTOList(List dtoList){
		BiWeekViewList viewList = null;
		
		BiWeekView biWeekView = null;
		
		viewList = new BiWeekViewList(0);
		
		if (!CollectionUtils.isEmpty(dtoList)){
			Iterator iterator = dtoList.iterator();
			while(iterator.hasNext()){
				BiWeekDTO e = (BiWeekDTO) iterator.next();
				biWeekView = convert(e);
				if(biWeekView.isDefaultInit()){
					viewList.setInitBiWeekDefaultId(biWeekView.getBiweekPeriodId());
				}else if(biWeekView.isDefaultFinish()){
					viewList.setFinishBiWeekDefaultId(biWeekView.getBiweekPeriodId());
				}
				
				viewList.add(biWeekView);
			}
		}
		
		return viewList;
	}
	
	/**
	 * 
	 * @param viewList
	 * @return
	 */
	public static List convertViewList(List viewList){
		List dtoList = null;
		
		dtoList = new ArrayList(0);
		
		if (!CollectionUtils.isEmpty(viewList)){
				Iterator iterator = viewList.iterator();
				while(iterator.hasNext()){
					BiWeekView e = (BiWeekView)iterator.next();				
					dtoList.add(convert(e));
			}
		}
		
		return dtoList;
	}
}
